package com.program.facesigninsystem.controller;

import com.github.pagehelper.PageInfo;
import com.program.facesigninsystem.entity.ResponseModel;
import com.program.facesigninsystem.iteration.dto.TeacherInfo;
import com.program.facesigninsystem.iteration.entity.NoteEntity;
import com.program.facesigninsystem.service.ITeacherService;
import com.program.facesigninsystem.service.NTeacherService;
import com.program.facesigninsystem.util.PageInfoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

@Api(description = "教师管理")
@RestController
@RequestMapping(value = "/teacher")
public class TeacherController {

    private static Logger log = Logger.getLogger(TeacherController.class.toString());
    @Resource
    ITeacherService ITeacherService;
    @Resource
    NTeacherService nTeacherService;

    private static PageInfoUtil pageInfoUtil;

    @ApiOperation("查询教师信息（通过uid）")
    @GetMapping("/selectTeacherById")
    public ResponseModel selectTeacherByuid(@ApiParam(value = "用户ID",required = true)@RequestParam String uid){
        TeacherInfo teacherInfo = nTeacherService.selectListByid(uid);
        if(teacherInfo != null ){
            return new ResponseModel(200,"查询成功",teacherInfo);
        }
        return new ResponseModel(199,"该教师不存在");
    }

    /*@ApiOperation("查询教师信息")
    @GetMapping("/selectList")
    public ResponseModel selectTeacherByuid(@RequestParam("name") String name,@RequestParam("university") String name,@RequestParam("name") String name){
        List<TeacherInfo> teacherInfoList = nTeacherService.selectListByid(uid);
        if(teacherInfoList != null ){
            return new ResponseModel(200,"查询成功",teacherInfoList);
        }
        return new ResponseModel(199,"该教师不存在");
    }*/

    /*@RequestMapping(value = "/selectTeacherById", method = RequestMethod.POST)
    public ResponseModel selectTeacherById(@RequestBody JSONObject data) {
        String uid = data.get("uid").toString();
        Map<String, Object> userInfo = (Map<String, Object>) ITeacherService.selectList(uid);
        ResponseModel responseModel;
        if (userInfo != null) {
            userInfo.putIfAbsent("name", "");
            userInfo.putIfAbsent("sex", "");
            userInfo.putIfAbsent("age", 0);
            userInfo.putIfAbsent("email", "");
            userInfo.putIfAbsent("bindId", 0);
            userInfo.putIfAbsent("university", "");
            userInfo.putIfAbsent("academy", "");
            userInfo.putIfAbsent("major", "");
            userInfo.putIfAbsent("note", "");
            responseModel = new ResponseModel(200, userInfo);
        } else {
            responseModel = new ResponseModel(199, "该教师不存在");
        }
        return responseModel;
    }*/
    @ApiOperation("查询教师信息(任意字段)")
    @RequestMapping(value = "/selectList", method = RequestMethod.GET)
    public ResponseModel selectList(HttpServletResponse resp,
                           @RequestParam(value = "name", required = false,defaultValue = "") String name,
                           @RequestParam(value = "university", required = false,defaultValue = "") String university,
                           @RequestParam(value = "academy", required = false,defaultValue = "") String academy,
                           @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {



        List<TeacherInfo> teacherInfoList = nTeacherService.selectList(name, university, academy);
        PageInfo pageInfo = PageInfoUtil.list2PageInfo(teacherInfoList,pageNum,pageSize);

        return new ResponseModel(200,"查询成功",pageInfo);

        /*try {
            if(name.contains(" ")||name.contains("'")||name.contains("\"")
                    ||university.contains(" ")||university.contains("'")||university.contains("\"")
                    ||academy.contains(" ")||academy.contains("'")||academy.contains("\"")){
                return;
            }

            List<Map<String, Object>> userlist = ITeacherService.selectList(name, university, academy);
            for (Map<String, Object> map : userlist) {
                map.putIfAbsent("name", "");
                map.putIfAbsent("sex", "");
                map.putIfAbsent("age", 0);
                map.putIfAbsent("email", "");
                map.putIfAbsent("bindId", 0);
                map.putIfAbsent("university", "");
                map.putIfAbsent("academy", "");
                map.putIfAbsent("major", "");
                map.putIfAbsent("note", "");
            }
            try {
                JSONObject data1 = PageUtil.wrap2(userlist,"User", pageSize, pageNum);
                resp.setCharacterEncoding("utf-8");
                PrintWriter respWritter = resp.getWriter();
                respWritter.append(data1.toString());
            } catch (Exception e) {
                log.log(Level.SEVERE, "Error parsing date", e);
            }
        } catch (Exception e) {
            log.log(Level.SEVERE, "Error parsing date", e);
        }*/
    }
    @ApiOperation("导出教师信息")
    @GetMapping("/exportList")
    public void exportListinfo(HttpServletResponse response){
        try {
            nTeacherService.exportExcel(response);
        }catch (Exception e){
            log.log(Level.SEVERE, "Error parsing date", e);
        }
    }


    /*@PostMapping("/exportList")
    public void exportList(@RequestBody ExcelParam excelParam,
                                     HttpServletRequest request,
                                     HttpServletResponse response) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String time = dateFormat.format(System.currentTimeMillis());
            String fileName = time + ".xls";
            ITeacherService.exportExcel(fileName, excelParam.getLid(), request, response, excelParam.getNoSignIn());
        } catch (Exception e) {
            log.log(Level.SEVERE, "Error parsing date", e);
        }
    }*/
    @ApiOperation("添加标签(疑似弃用)")
    @PostMapping("/addNote")
    public ResponseModel addNote(@RequestParam("uid") String uid,@RequestParam("note") String note){

        try{
            nTeacherService.addnote(uid, note);
            return new ResponseModel(200,"增加成功");
        }catch (Exception e){
            return new ResponseModel(400,e.getMessage());
        }
    }

    @ApiOperation("获取标签(疑似弃用)")
    @GetMapping("/getNote")
    public ResponseModel getNote(@RequestParam String uid){

        try{
            List<NoteEntity> list = nTeacherService.getnotebyid(uid);
            return new ResponseModel(200,"查询成功",list);
        }catch (Exception e){
            return new ResponseModel(400,e.getMessage());
        }
    }
   /* @PostMapping("/addNote")
    public ResponseModel addNote(@RequestBody UserEntity user) {

        if (ITeacherService.addNote(user) == 1)
            return new ResponseModel(200,"添加成功");
        else
            return new ResponseModel(500, "添加失败");
    }*/
}